//
//  HNChangePwdController.m
//  baidaifu
//
//  Created by 朱传正 on 2017/1/3.
//  Copyright © 2017年 haoniu. All rights reserved.
//

#import "HNChangePwdController.h"
#import "NSString+RegexCategory.h"

@interface HNChangePwdController ()

@end

@implementation HNChangePwdController

-(void)drawView{
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGColorRef color = CGColorCreate(colorSpaceRef, (CGFloat[]){189.0/255.0,189.0/255.0,189.0/255.0,1});
    
    
    _originPwdView.layer.borderColor = color;
    _originPwdView.layer.borderWidth = 0.5;
    
    _currentPwdView1.layer.borderColor = color;
    _currentPwdView1.layer.borderWidth = 0.5;
    
    _currentPwdView2.layer.borderColor = color;
    _currentPwdView2.layer.borderWidth = 0.5;
}


- (IBAction)changePwdbtn:(UIButton *)sender {
    
    [self updatePwd];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改电话";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - updateUserInfo 更新密码
-(void)updatePwd{
    if ([AppConfig isNULL:_originPwd.text]) {
        [self showHint:@"请输入原密码"];
        return;
    }
    if ([AppConfig isNULL:_currentPwd.text]) {
        [self showHint:@"请输入新密码"];
        return;
    }
    if ([AppConfig isNULL:_currentPwd2.text]) {
        [self showHint:@"请再次输入新密码"];
        return;
    }
    
//    NSString * regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
    NSString * regex = @"[A-Za-z0-9]{6,16}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    if (![pred evaluateWithObject:_currentPwd.text]){
        [self showHint:@"请输入6-16位密码"];
        return;
    }
    
    if (![_currentPwd.text isEqualToString:_currentPwd2.text]) {
        [self showHint:@"输入的两次新密码不一致"];
        return;
    }
    
    if ([_originPwd.text isEqualToString:_currentPwd2.text]) {
        [self showHint:@"新密码与原密码不能一样"];
        return;
    }
    
    
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:static_sessionId,@"sessionId",nil];
    
    [param setObject:_originPwd.text forKey:@"password"];
    [param setObject:_currentPwd.text forKey:@"newPassword"];
    
    NSLog(@"param = %@",param);
    
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_update_pwd_pwd parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSLog(@"responseObject = %@",responseObject);
        
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"修改密码失败"];
            NSLog(@"==dict:%@",data);
            return;
        }
        NSLog(@"==dict:%@",data);
        
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
//            _userInfo = [NSDictionary dictionaryWithDictionary:[data objectForKey:@"obj"]];
//            NSLog(@"------obj = %@",_userInfo);
            
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            if ([info isEqualToString:msg_InvalidLoginInformation]) {
                
                [AppConfig removeUserInfo];
                
                [[EMClient sharedClient] logout:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                
                [self.navigationController pushViewController:loginVC animated:YES];
            }
            [self showHint:info];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull  error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"修改密码失败"];
    }];
    
}



@end
